<!DOCTYPE html>

<html lang="en">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width">
	<title>Common Modules Source: control/compareLayer.js</title>

	<!--[if lt IE 9]>
	<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
	<![endif]-->
	<link type="text/css" rel="stylesheet" href="styles/sunlight.default.css">

	<link type="text/css" rel="stylesheet" href="styles/site.simplex.css">

</head>

<body>

<div class="navbar navbar-default navbar-fixed-top navbar-inverse">
<div class="container">
	<div class="navbar-header">
		<a class="navbar-brand" href="index.html">Common Modules</a>
		<button class="navbar-toggle" type="button" data-toggle="collapse" data-target="#topNavigation">
			<span class="icon-bar"></span>
			<span class="icon-bar"></span>
			<span class="icon-bar"></span>
        </button>
	</div>
	<div class="navbar-collapse collapse" id="topNavigation">
		<ul class="nav navbar-nav">
			
			<li class="dropdown">
				<a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b class="caret"></b></a>
				<ul class="dropdown-menu ">
					<li><a href="Base.html">Base</a></li><li><a href="Event.html">Event</a></li><li><a href="Html.html">Html</a></li><li><a href="Internal.html">Internal</a></li><li><a href="ol.Overlay.Popup.html">ol.Overlay.Popup</a></li>
				</ul>
			</li>
			
			<li class="dropdown">
				<a href="global.html" class="dropdown-toggle" data-toggle="dropdown">Global<b class="caret"></b></a>
				<ul class="dropdown-menu ">
					<li><a href="global.html#addClass">addClass</a></li><li><a href="global.html#addListener">addListener</a></li><li><a href="global.html#arrayRemove">arrayRemove</a></li><li><a href="global.html#clear">clear</a></li><li><a href="global.html#create">create</a></li><li><a href="global.html#createHidden">createHidden</a></li><li><a href="global.html#empty">empty</a></li><li><a href="global.html#EventType">EventType</a></li><li><a href="global.html#findListener">findListener</a></li><li><a href="global.html#firstUpperToCase">firstUpperToCase</a></li><li><a href="global.html#get">get</a></li><li><a href="global.html#getChildByTagName">getChildByTagName</a></li><li><a href="global.html#getClass">getClass</a></li><li><a href="global.html#getDomEventKey">getDomEventKey</a></li><li><a href="global.html#getElementsByClassName">getElementsByClassName</a></li><li><a href="global.html#getListeners">getListeners</a></li><li><a href="global.html#getrandom">getrandom</a></li><li><a href="global.html#getStyle">getStyle</a></li><li><a href="global.html#getuuid">getuuid</a></li><li><a href="global.html#hasClass">hasClass</a></li><li><a href="global.html#isObject">isObject</a></li><li><a href="global.html#isString">isString</a></li><li><a href="global.html#layerConfigDefautls">layerConfigDefautls</a></li><li><a href="global.html#listen">listen</a></li><li><a href="global.html#listenOnce">listenOnce</a></li><li><a href="global.html#logo">logo</a></li><li><a href="global.html#OL_Style_Type">OL_Style_Type</a></li><li><a href="global.html#remove">remove</a></li><li><a href="global.html#removeClass">removeClass</a></li><li><a href="global.html#removeListener">removeListener</a></li><li><a href="global.html#removeListeners">removeListeners</a></li><li><a href="global.html#setClass">setClass</a></li><li><a href="global.html#splitWords">splitWords</a></li><li><a href="global.html#stamp">stamp</a></li><li><a href="global.html#toBack">toBack</a></li><li><a href="global.html#toFront">toFront</a></li><li><a href="global.html#trim">trim</a></li><li><a href="global.html#unListen">unListen</a></li><li><a href="global.html#unlistenAll">unlistenAll</a></li><li><a href="global.html#unListenByKey">unListenByKey</a></li><li><a href="global.html#upperFirstChart">upperFirstChart</a></li>
				</ul>
			</li>
			
		</ul>
        
            <div class="col-sm-3 col-md-3">
                <form class="navbar-form" role="search">
                    <div class="input-group">
                        <input type="text" class="form-control" placeholder="Search" name="q" id="search-input">
                        <div class="input-group-btn">
                            <button class="btn btn-default" id="search-submit"><i class="glyphicon glyphicon-search"></i></button>
                        </div>
                    </div>
                </form>
            </div>
        
	</div>

</div>
</div>


<div class="container" id="toc-content">
<div class="row">

	
	<div class="col-md-12">
	
		<div id="main">
			

		<h1 class="page-title">Source: control/compareLayer.js</h1>
    
<section>
    <article>
        <pre
            class="sunlight-highlight-javascript linenums">/**
 * @desc 用于图层比较
 */
import {
  ol
} from '../constants'
import '../asset/scss/compareLayer'
import {BASE_CLASS_NAME} from '../constants'
import * as htmlUtils from '../utils/domUtils'
import * as Events from '../event/Events'
import 'pepjs'
ol.control.CompareLayer = function (beforeMap, afterMap, params) {
  this.options = params || {}
  if (beforeMap &amp;&amp; afterMap) {
    this.beforeMap = beforeMap
    this.afterMap = afterMap
  } else {
    throw new Error('图层必须传入！')
  }

  this.orderLayerZindex()

  this.className = (this.options.className !== undefined ? this.options.className : 'gmap-compare')

  /**
   * 当前截取位置占整个视图宽度的比例
   * @type {number}
   */
  this.initPosition = (this.options['initPosition'] !== undefined ? this.options['initPosition'] : 0.5)

  /**
   * 是否正在拖拽
   * @type {boolean}
   * @private
   */
  this.dragging_ = false

  /**
   * 原始X
   * @type {null}
   * @private
   */
  this.previousX_ = null

  /**
   * 原始Y
   * @type {null}
   * @private
   */
  this.previousY_ = null

  /**
   * @private
   * @type {Element}
   */
  this.element_ = htmlUtils.create('div', (this.className + ' ' + BASE_CLASS_NAME.CLASS_UNSELECTABLE))

  /**
   * @private
   * @type {Element}
   */
  let innerElement_ = htmlUtils.create('div', this.className + '-inner', this.element_)

  Events.listen(innerElement_, 'pointerdown', this.handleDraggerStart_, this)
  Events.listen(innerElement_, 'pointermove', this.handleDraggerDrag_, this)
  Events.listen(innerElement_, 'pointerup', this.handleDraggerEnd_, this)
  Events.listen(window, 'pointerup', this.handleDraggerEnd_, this)

  ol.control.Control.call(this, {
    element: this.element_,
    target: this.options['target']
  })
}

ol.inherits(ol.control.CompareLayer, ol.control.Control)

/**
 * setup
 */
ol.control.CompareLayer.prototype.initControl = function () {
  /**
   * 获取当前视图大小
   * @type {ClientRect}
   * @private
   */
  this._bounds = this.getMap().getTargetElement().getBoundingClientRect()
  this.percent = 0.5
  this._setPosition(this._bounds.width, this._bounds.width / 2)
  this.resize()
  this.clipLayer()
}

/**
 * 处理拖拽
 * @param event
 * @private
 */
ol.control.CompareLayer.prototype.handleDraggerStart_ = function (event) {
  if (!this.dragging_ &amp;&amp; event.target === htmlUtils.getElementsByClassName('.' + this.className + '-inner', this.element_)) {
    this.previousX_ = event.clientX
    this.previousY_ = event.clientY
    this.dragging_ = true
  }
}

/**
 * 处理拖动事件
 * @param event
 * @private
 */
ol.control.CompareLayer.prototype.handleDraggerDrag_ = function (event) {
  if (this.dragging_) {
    this._bounds = this.getMap().getTargetElement().getBoundingClientRect()
    this._setPosition(this._bounds.width, this._getX(event))
    this.previousX_ = event.clientX
    this.previousY_ = event.clientY
  }
}

/**
 * 处理拖拽结束事件
 * @param event
 * @private
 */
ol.control.CompareLayer.prototype.handleDraggerEnd_ = function (event) {
  if (this.dragging_) {
    this.dragging_ = false
    this.previousX_ = undefined
    this.previousY_ = undefined
  }
}

/**
 * 通过canvas切割视图
 */
ol.control.CompareLayer.prototype.clipLayer = function () {
  let that = this
  this.getMap().un('precompose', this.precompose)
  this.getMap().un('postcompose', this.postcompose)
  this.precompose = this.beforeMap.on('precompose', event => {
    let ctx = event.context
    let width = ctx.canvas.width * (that.initPosition)
    ctx.save()
    ctx.beginPath()
    ctx.rect(width, 0, ctx.canvas.width - width, ctx.canvas.height)
    ctx.clip()
  })
  this.postcompose = this.beforeMap.on('postcompose', event => {
    let ctx = event.context
    ctx.restore()
  })
}

/**
 * 设置切割位置
 * @param sourceWidth
 * @param value
 * @private
 */
ol.control.CompareLayer.prototype._setPosition = function (sourceWidth, value) {
  let pos = 'translate(' + value + 'px, 0)'
  this.element_.style.transform = pos
  this.element_.style.WebkitTransform = pos
  this._x = value
  this.percent = value / sourceWidth
  this.initPosition = value / sourceWidth
  this.getMap().render()
}

/**
 * 窗口变化事件
 */
ol.control.CompareLayer.prototype.resize = function () {
  let resizeEvt = (('orientationchange' in window) ? 'orientationchange' : 'resize')
  let doc = window.document
  let that = this
  window.addEventListener(resizeEvt, function () {
    setTimeout(function () {
      that._bounds = that.getMap().getTargetElement().getBoundingClientRect()
      that._setPosition(that._bounds.width, that._bounds.width * that.percent)
    }, 50)
  }, false)
  window.addEventListener('pageshow', function (e) {
    if (e.persisted) {
      setTimeout(function () {
        that._bounds = that.getMap().getTargetElement().getBoundingClientRect()
        that._setPosition(that._bounds.width, that._bounds.width * that.percent)
      }, 50)
    }
  }, false)
  if (doc.readyState === 'complete') {
    setTimeout(function () {
      that._bounds = that.getMap().getTargetElement().getBoundingClientRect()
      that._setPosition(that._bounds.width, that._bounds.width * that.percent)
    }, 50)
  } else {
    doc.addEventListener('DOMContentLoaded', function (e) {
      setTimeout(function () {
        that._bounds = that.getMap().getTargetElement().getBoundingClientRect()
        that._setPosition(that._bounds.width, that._bounds.width * that.percent)
      }, 50)
    }, false)
  }
}

/**
 * 获取当前位置
 * @param e
 * @returns {number}
 * @private
 */
ol.control.CompareLayer.prototype._getX = function (e) {
  e = e.touches ? e.touches[0] : e
  let x = e.clientX - this._bounds.left
  if (x &lt; 0) x = 0
  if (x > this._bounds.width) x = this._bounds.width
  return x
}

/**
 * 设置地图
 * @param map
 */
ol.control.CompareLayer.prototype.setMap = function (map) {
  if (map &amp;&amp; map instanceof ol.Map) {
    ol.control.Control.prototype.setMap.call(this, map)
    if (map) {
      map.render()
      this.initControl()
    }
  } else {
    throw Error('传入的不是地图对象！')
  }
}

/**
 * 设置上一级图层
 * @param beforeMap
 */
ol.control.CompareLayer.prototype.setBeforeLayet = function (beforeMap) {
  if (beforeMap) {
    this.beforeMap = beforeMap
    this.orderLayerZindex()
  } else {
    throw Error('设置图层错误！')
  }
}

/**
 * 设置下一级图层
 * @param afterMap
 */
ol.control.CompareLayer.prototype.setAfterLayer = function (afterMap) {
  if (afterMap) {
    this.afterMap = afterMap
    this.orderLayerZindex()
  } else {
    throw Error('设置图层错误！')
  }
}

/**
 * 调整相关图层层级，避免图层压盖
 */
ol.control.CompareLayer.prototype.orderLayerZindex = function () {
  if (this.afterMap &amp;&amp; this.beforeMap) {
    let afterMapIndex = this.afterMap.getZIndex()
    let beforeMapIndex = this.beforeMap.getZIndex()
    let max = Math.max(afterMapIndex, beforeMapIndex)
    let min = Math.min(afterMapIndex, beforeMapIndex)
    if (max === min) {
      max = max + 1
    }
    this.beforeMap.setZIndex(max)
    this.afterMap.setZIndex(min)
  }
}

let olControlCompareLayer = ol.control.CompareLayer
export default olControlCompareLayer
</pre>
    </article>
</section>





		</div>
	</div>

	<div class="clearfix"></div>

	

</div>
</div>


    <div class="modal fade" id="searchResults">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">Search results</h4>
          </div>
          <div class="modal-body"></div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          </div>
        </div><!-- /.modal-content -->
      </div><!-- /.modal-dialog -->
    </div>


<footer>


	<span class="copyright">
	https://lzw.me.
	</span>

<span class="jsdoc-message">
	Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a>
	
		on Sun Aug 12th 2018
	
	using the <a href="https://github.com/docstrap/docstrap">DocStrap template</a>.
</span>
</footer>

<script src="scripts/docstrap.lib.js"></script>
<script src="scripts/toc.js"></script>

    <script type="text/javascript" src="scripts/fulltext-search-ui.js"></script>


<script>
$( function () {
	$( "[id*='$']" ).each( function () {
		var $this = $( this );

		$this.attr( "id", $this.attr( "id" ).replace( "$", "__" ) );
	} );

	$( ".tutorial-section pre, .readme-section pre, pre.prettyprint.source" ).each( function () {
		var $this = $( this );

		var example = $this.find( "code" );
		exampleText = example.html();
		var lang = /{@lang (.*?)}/.exec( exampleText );
		if ( lang && lang[1] ) {
			exampleText = exampleText.replace( lang[0], "" );
			example.html( exampleText );
			lang = lang[1];
		} else {
			var langClassMatch = example.parent()[0].className.match(/lang\-(\S+)/);
			lang = langClassMatch ? langClassMatch[1] : "javascript";
		}

		if ( lang ) {

			$this
			.addClass( "sunlight-highlight-" + lang )
			.addClass( "linenums" )
			.html( example.html() );

		}
	} );

	Sunlight.highlightAll( {
		lineNumbers : true,
		showMenu : true,
		enableDoclinks : true
	} );

	$.catchAnchorLinks( {
        navbarOffset: 10
	} );
	$( "#toc" ).toc( {
		anchorName  : function ( i, heading, prefix ) {
			return $( heading ).attr( "id" ) || ( prefix + i );
		},
		selectors   : "#toc-content h1,#toc-content h2,#toc-content h3,#toc-content h4",
		showAndHide : false,
		smoothScrolling: true
	} );

	$( "#main span[id^='toc']" ).addClass( "toc-shim" );
	$( '.dropdown-toggle' ).dropdown();

    $( "table" ).each( function () {
      var $this = $( this );
      $this.addClass('table');
    } );

} );
</script>



<!--Navigation and Symbol Display-->


<!--Google Analytics-->



    <script type="text/javascript">
        $(document).ready(function() {
            SearcherDisplay.init();
        });
    </script>


</body>
</html>
